package com.example.myapplication

import android.annotation.SuppressLint
import android.hardware.usb.UsbDevice
import android.os.Bundle
import android.util.Log
import android.view.SurfaceHolder
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.herohan.uvcapp.CameraHelper
import com.herohan.uvcapp.ICameraHelper
import com.serenegiant.usb.IFrameCallback
import com.serenegiant.usb.UVCCamera.PIXEL_FORMAT_NV12
import com.serenegiant.widget.AspectRatioSurfaceView
import java.nio.ByteBuffer

class MainActivity : AppCompatActivity() {
    private var mSurface:AspectRatioSurfaceView? = null
    private var mCameraHelper:CameraHelper?=null
    private var mStateListener:ICameraHelper.StateCallback?=null

    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        mSurface = findViewById(R.id.asSurfaceView)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        initview()

    }

    private fun initview() {
        mStateListener = object :ICameraHelper.StateCallback{
            override fun onAttach(device: UsbDevice?) {
                mCameraHelper?.selectDevice(device)
            }

            override fun onDeviceOpen(device: UsbDevice?, isFirstOpen: Boolean) {

                mCameraHelper?.openCamera()

            }

            override fun onCameraOpen(device: UsbDevice?) {
                mCameraHelper?.startPreview()
                val size = mCameraHelper?.previewSize
                val width = size?.width?:0
                val height = size?.height?:0
                if(width>0 && height>0)
                mSurface?.setAspectRatio(width,height)
                mCameraHelper?.addSurface(mSurface?.holder?.surface,false)


            }

            override fun onCameraClose(device: UsbDevice?) {
                mCameraHelper?.removeSurface(mSurface?.holder?.surface)
            }

            override fun onDeviceClose(device: UsbDevice?) {
            }

            override fun onDetach(device: UsbDevice?) {
            }

            override fun onCancel(device: UsbDevice?) {
            }

        }

        mSurface?.holder?.addCallback(object :SurfaceHolder.Callback{
            override fun surfaceCreated(holder: SurfaceHolder) {
                mCameraHelper?.addSurface(holder?.surface,false)
            }

            override fun surfaceChanged(
                holder: SurfaceHolder,
                format: Int,
                width: Int,
                height: Int
            ) {
            }

            override fun surfaceDestroyed(holder: SurfaceHolder) {
                mCameraHelper?.removeSurface(holder?.surface)
            }

        })

        mCameraHelper = CameraHelper()
        mCameraHelper?.setStateCallback(mStateListener)





    }
}